package net.dgg.zz.credential.company.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.idutil.DggKeyWorker;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.CustomerNumberEncryptionUtil;
import net.dgg.iboss.base.util.DesUtil;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.zz.common.service.CommonService;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.zz.common.utils.DateUtils;
import net.dgg.zz.common.utils.excel.ExcelUtils;
import net.dgg.zz.credential.company.entity.Company;
import net.dgg.zz.credential.company.service.CompanyService;
import net.dgg.zz.credential.company.vo.CompanyVo;
import net.dgg.zz.credential.talent.vo.TalentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="/zs/com")
public class CompanyController extends DggBaseController {
    @Autowired
    private CommonService commonService;
    @Autowired
    private CompanyService companyService;
    @Autowired
    private DesUtil desUtil;

    /**
     * 公司页面
     * @param model
     * @return
     */
    @RequestMapping(value="/index")
    public String index(Model model){
        model.addAttribute("scale",commonService.queryTreeBook("SCALE",1,2,null));
        model.addAttribute("talentPosition",commonService.queryTreeBook("TALENT_POSITION",1,2,null));
        model.addAttribute("coopStatus",commonService.queryTreeBook("COOP_STATUS",1,2,null));
        model.addAttribute("tracker",companyService.findTracker());
        return "zzsc/credential/company/index";
    }

    /**
     * 公司列表
     * @param params
     * @param request
     * @return
     */
    @RequestMapping(value="/com_list")
    @ResponseBody
    public DataTableResponse queryTalentList(@RequestParam Map params, HttpServletRequest request){
        try {
            DataTableRequest r = DataTableUtils.getParam(request); //获取datatable参数
            params.put("start", r.getStart());//获取当前页
            params.put("limit", r.getLength());//获取每页条数
            List<Company> list = companyService.selectComByWithPage(params);
            Integer count = companyService.count(params);
            return new DataTableResponse(r.getDraw(), count, count, list, "");
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    @RequestMapping(value="/creatCom")
    public String createTalent(Model model){
        model.addAttribute("id", DggKeyWorker.nextId());
        model.addAttribute("scale",commonService.queryTreeBook("SCALE",1,2,null));
        model.addAttribute("talentPosition",commonService.queryTreeBook("TALENT_POSITION",1,2,null));
        model.addAttribute("coopStatus",commonService.queryTreeBook("COOP_STATUS",1,2,null));
        return "zzsc/credential/company/create";
    }

    @RequestMapping(value="/addCom")
    @ResponseBody
    public DggRestResponse addTalent(@RequestParam Map params){
        try {
            boolean isExist = companyService.isExist(params);
            if(isExist){
                return this.getFailResponse("企业库已存在相同的合同！");
            }
            companyService.insertCompany(params);
            return this.getSuccessResponse("保存成功！");
        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }
    @RequestMapping(value="/saveCom")
    @ResponseBody
    public DggRestResponse edtTalent(@RequestParam Map params){
        try {
            boolean isExist = companyService.isExistEdit(params);
            if(isExist){
                return this.getFailResponse("企业库已存在相同的合同！");
            }
            companyService.updateCompany(params);
            return this.getSuccessResponse("保存成功");
        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }
    @RequestMapping(value="/editCom")
    public String editTalent(Model model,Long id){
        model.addAttribute("scale",commonService.queryTreeBook("SCALE",1,2,null));
        model.addAttribute("talentPosition",commonService.queryTreeBook("TALENT_POSITION",1,2,null));
        model.addAttribute("coopStatus",commonService.queryTreeBook("COOP_STATUS",1,2,null));
        Company company = companyService.getCompanyById(id);
        model.addAttribute("company",company);
        return "zzsc/credential/company/edit";
    }

    @RequestMapping(value="/see_number")
    @ResponseBody
    public DggRestResponse seeNumber(@RequestParam Long id){
        try {
            String returnStr = companyService.getContactPhoneById(id);
            return this.getSuccessResponse(returnStr);

        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }
    @RequestMapping(value="/find_com")
    @ResponseBody
    public DggRestResponse findTalentById(@RequestParam Long id){
        try {
           Company company = companyService.getCompanyByIdNotDe(id);
            Map<String, Object> talentInfo = new HashMap<>();
            talentInfo.put("id", company.getId());
            talentInfo.put("name", company.getContactName());
            Map noMap = new HashMap();
            noMap.put("phoneStr",desUtil.decrypt(company.getContactPhone()));
            noMap = CustomerNumberEncryptionUtil.encryptionObject(new String[]{"phoneStr"},noMap);
            talentInfo.put("phone", company.getContactPhone());
            talentInfo.put("phoneStr", noMap.get("phoneStr"));
            return this.getSuccessResponse(talentInfo);
        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }

    @RequestMapping(value="/downLoadFile")
    public String downLoadFile(Model model,Long id){
        model.addAttribute("id", id);
        return "zzsc/credential/company/file";
    }

    /**
     * 分配跟踪人
     * @param model
     * @return
     */
    @RequestMapping(value="/trackerPage")
    public String trackerPage(Model model,Long id){
        model.addAttribute("id",id);
        return "zzsc/credential/company/tracker";
    }

    /**
     * 更新跟踪人，合同状态
     * @param params
     * @return
     */
    @RequestMapping(value="/update")
    @ResponseBody
    public DggRestResponse update(@RequestParam Map params){
        try {
            companyService.updateCompanyTracker(params);
            return this.getSuccessResponse("保存成功");
        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }
    @RequestMapping(value="/statePage")
    public String statePage(Model model,Long id){
        model.addAttribute("id",id);
        model.addAttribute("coopStatus",commonService.queryTreeBook("COOP_STATUS",1,2,null));
        return "zzsc/credential/company/state";
    }

    @RequestMapping(value="/viewJobDuty")
    public String viewJobDuty(Model model,Long id){
        model.addAttribute("company",companyService.getCompanyById(id));
        model.addAttribute("id", id);
        return "zzsc/credential/company/jobDuty";
    }

    @RequestMapping("/export_company")
    public void exportSczzAbnormal(@RequestParam Map params, HttpServletResponse response) {
        try {
            String fileName = "企业库_" + DateUtils.dateString(new Date(), "yyyyMMddHHmm") + ".xlsx";
            List<CompanyVo> dataList = companyService.findExportCompany(params);
            ExcelUtils.writeToFile(dataList, null, fileName, response, CompanyVo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
